package thread;

import java.util.*;

public class SyncAPIDemo2 {
    public static void main(String[] args) {
        //List<Integer> c= new ArrayList<>();
        //List<Integer> c= new LinkedList<>();
        //Set<Integer> c = new HashSet<>();
        //List<Integer> c=Collections.synchronizedList(new ArrayList<>());//并发安全
        //List<Integer> c=Collections.synchronizedList(new LinkedList<>());//并发安全
        Set<Integer> c=Collections.synchronizedSet(new HashSet<>());//并发安全
        /*继承Thread，重写run方法*/
        Thread t1= new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++) {
                    c.add(i);
                }
            }
        };
        Thread t2= new Thread(){
            public void run(){
                for (int i = 0; i < 1000; i++) {
                    c.add(i*2);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {

        }
        System.out.println(c.size());
        System.out.println(c);
    }
}
